package com.itheima.controller;/*
@Author:李正铠
@Date:2020年04月15日20时25分
*/

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.AuthorityManageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 权限管理
 */
@Controller
@ResponseBody
@RequestMapping("/authorityManage")
public class AuthorityManageController {

    @Reference
    private AuthorityManageService authorityManageService;

    /**
     * 查询所有权限数据
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('AUTHORITY_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = authorityManageService.findPage(queryPageBean);
            return new Result(true, MessageConstant.GET_AUTHORITYMANAGE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_AUTHORITYMANAGE_FAIL);
        }
    }

    /**
     * 新增权限
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("hasAuthority('AUTHORITY_ADD')")
    public Result add(@RequestBody Permission permission){
        try {
            authorityManageService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_UTHORITYMANAGE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_UTHORITYMANAGE_SUCCESS);
    }


    /**
     * 编辑权限数据回显
     */
    @RequestMapping(value = "/fingPermissionById")
    @PreAuthorize("hasAuthority('AUTHORITY_EDIT')")
    public Result fingPermissionById(Integer permissionId){
        try {
           Permission permissionList=  authorityManageService.fingPermissionById(permissionId);
        return new Result(true,MessageConstant.GET_AUTHORITYMANAGE_SUCCESS,permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_AUTHORITYMANAGE_FAIL);
        }
    }

    /**
     * 编辑权限
     */
    @RequestMapping(value = "/update")
    @PreAuthorize("hasAuthority('AUTHORITY_EDIT')")
    public Result update(@RequestBody Permission permission){
        try {
            authorityManageService.update(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_UTHORITYMANAGE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_UTHORITYMANAGE_SUCCESS);
    }

    /**
     * 删除权限数据和对应关系数据
     */
    @RequestMapping(value = "/delete")
    @PreAuthorize("hasAuthority('AUTHORITY_DELETE')")
    public Result delete(Integer permissionId){
        try {
            authorityManageService.delete(permissionId);
            return new Result(true,MessageConstant.DELETE_UTHORITYMANAGE_SUCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_UTHORITYMANAGE_FAIL);
        }
    }

    /**
     * 查询所有权限数据
     */
    @RequestMapping(value = "/findAll")
    @PreAuthorize("hasAnyAuthority('ROLE_ADD','ROLE_EDIT')")
    public Result findAll(){
        List<Permission> permissionTableData = null;
        try {
            permissionTableData = authorityManageService.findAll();
        return new Result(true,MessageConstant.GET_AUTHORITYMANAGE_SUCCESS,permissionTableData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_AUTHORITYMANAGE_FAIL);
        }
    }




}
